package com.atguigu.gmallre.mock.bean;

import com.atguigu.gmallre.mock.constant.GmallConstant;
import com.atguigu.gmallre.mock.factory.UserSession;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券领用表
 * </p>
 *
 * @author zhangchen
 * @since 2020-02-26
 */
@Data
public class CouponUse implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 编号
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 购物券ID
     */
    private Long couponId;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 订单ID
     */
    private Long orderId;

    /**
     * 购物券状态
     */
    private String couponStatus;

    /**
     * 领券时间
     */
    private Date getTime;

    /**
     * 使用时间
     */
    private Date usingTime;

    /**
     * 支付时间
     */
    private Date usedTime;

    /**
     * 过期时间
     */
    private Date expireTime;



    /**
     * 创建时间
     */
    private Date createTime;


    /**
     * 修改时间
     */
    private Date operateTime;


    @TableField(exist = false)
    private OrderInfo orderInfo;

    @TableField(exist = false)
    private CouponInfo couponInfo;


    @TableField(exist = false)
    private BigDecimal orderSumAmount=BigDecimal.ZERO;
    @TableField(exist = false)
    private Long orderSkuNum=0L;

    @TableField(exist = false)
    private BigDecimal reduceAmount;


    @TableField(exist = false)
    private List<OrderDetailCoupon> orderDetailCouponList=new ArrayList<>();

    public void addOrderDetail(OrderDetail orderDetail){
        OrderDetailCoupon orderDetailCoupon = new OrderDetailCoupon();
        orderDetailCoupon.setOrderDetail(orderDetail);
        orderDetailCoupon.setCouponId(this.getCouponInfo().getId());
        orderDetailCoupon.setSkuId(orderDetail.getSkuId());
        orderDetailCoupon.setCouponUse(this);
        orderDetailCouponList.add(orderDetailCoupon);
        orderSumAmount=orderSumAmount.add(orderDetail.getSplitTotalAmount());
        orderSkuNum=orderSkuNum+orderDetail.getSkuNum() ;
    }


    public  boolean checkRequireCondition(){
        if(this.getCouponInfo().getCouponType().equals(GmallConstant.COUPON_TYPE_MJ)){
            return  orderSumAmount.compareTo(this.getCouponInfo().getConditionAmount()) >0 ;
        }else if(this.getCouponInfo().getCouponType().equals(GmallConstant.COUPON_TYPE_ML)) {
            return  orderSkuNum.compareTo(this.getCouponInfo().getConditionNum()) >0 ;
        }else if(this.getCouponInfo().getCouponType().equals(GmallConstant.COUPON_TYPE_DJ)) {
            return  true ;
        }
        return  false;
    }

    public BigDecimal calcReduceAmount(){

        if(this.getCouponInfo().getCouponType().equals(GmallConstant.COUPON_TYPE_MJ)){
            reduceAmount=  this.getCouponInfo().getBenefitAmount()  ;
        }else if(this.getCouponInfo().getCouponType().equals(GmallConstant.COUPON_TYPE_ML)) {

            for (OrderDetailCoupon orderDetailCoupon: orderDetailCouponList) {
                BigDecimal splitTotalAmount = orderDetailCoupon.getOrderDetail().getSplitTotalAmount();
                BigDecimal reduceRatio = BigDecimal.ONE.subtract(this.getCouponInfo().getBenefitDiscount().movePointLeft(1));
                BigDecimal benefitAmountDetail = splitTotalAmount.multiply(reduceRatio);
                reduceAmount = reduceAmount.add(benefitAmountDetail);
            }
        }else if(this.getCouponInfo().getCouponType().equals(GmallConstant.COUPON_TYPE_DJ)) {
            reduceAmount= this.getCouponInfo().getBenefitAmount() ;
        }
        return  reduceAmount ;
    }

    //把优惠金额分摊到各个订单明细
    public  void splitAmount(){
        List<OrderDetailCoupon> orderDetailCouponList = this.orderDetailCouponList;

        BigDecimal remainReduceAmount=reduceAmount;
        for (int i = 0; i < orderDetailCouponList.size(); i++) {
            OrderDetailCoupon orderDetailCoupon = orderDetailCouponList.get(i);
            OrderDetail orderDetail = orderDetailCoupon.getOrderDetail();
            if(i== orderDetailCouponList.size()-1 ){
                orderDetail.setSplitCouponAmount(remainReduceAmount);
                orderDetail.calcTotalAmount();
            }else{
                    //  reduceDetail =  reduceALL* orderdetailTotal / orderTotal
                BigDecimal reduceDetail = reduceAmount.multiply(orderDetail.getSplitTotalAmount()).divide(orderSumAmount,2, RoundingMode.HALF_UP);
                orderDetail.setSplitCouponAmount(reduceDetail);
                remainReduceAmount=remainReduceAmount.subtract(reduceDetail);
                orderDetail.calcTotalAmount();

            }


        }


    }


    public void  syncOrderInfo(){
        OrderInfo orderInfo = this.getOrderInfo();
        this.setOrderId(orderInfo.getId());
        this.setCouponStatus(GmallConstant.COUPON_STATUS_USING);
        this.setOperateTime(orderInfo.getCreateTime());
        this.setUsingTime(orderInfo.getCreateTime());




    }

    public void  syncOrderDetail(){
        for (OrderDetailCoupon orderDetailCoupon : orderDetailCouponList) {
            OrderDetail orderDetail = orderDetailCoupon.getOrderDetail();
            orderDetailCoupon.setOrderDetailId(orderDetail.getId());
            orderDetailCoupon.setOrderId(orderDetail.getOrderId());
            orderDetailCoupon.setCouponUseId(id);
        }
    }

}
